import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author wq3stone
 */
public class Page3Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = DBUtil.getConnection();
        String sql1 = "select num from tb_sum";
        int num1 = 0,num2 = 0;
        int sum = 0;
        try {
            ps = conn.prepareStatement(sql1);
            rs = ps.executeQuery();
            if(rs.next()) num1 = rs.getInt("num");
            if(rs.next()) num2 = rs.getInt("num");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        String sql2 = "select sum(num) from tb_sum";
        try {
            ps = conn.prepareStatement(sql2);
            rs = ps.executeQuery();
            if(rs.next()) sum = rs.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        resp.getWriter().print(num1+"+"+num2+"="+sum);
    }
}
